package cn.kgc.javaweb.servlet;

import cn.kgc.javaweb.entity.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;

/**
 * @author: mosin
 * @version: v1.0  2022/6/24
 */
@WebServlet("/user")
public class UserServlet extends BaseServlet {


    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //req.setCharacterEncoding("utf-8");

        String username = req.getParameter("username");
        String password = req.getParameter("password");

        System.out.println("username = " + username);

        if("admin".equals(username)&&"12345".equals(password)){

            ArrayList<Student> students = new ArrayList<>();
            for (int i = 0; i < 10; i++) {
                Student student = Student.builder().birth(new Date()).age(20 + i).stuName("张" + i).stuNo("202000" + i).build();
                students.add(student);
            }


            HttpSession session = req.getSession();
            session.setAttribute("students", students);

            //将用户数据存入 session 用于登录判定

            session.setAttribute("username", username);
            session.setAttribute("password", password);


            resp.sendRedirect(req.getContextPath()+"/StudentList.jsp");


        }else{

            req.setAttribute("info", "用户名或密码错误");
            req.getRequestDispatcher("login.jsp").forward(req, resp);

        }




    }

    protected void register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


    }


    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


    }


}
